{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#波士顿房价预测\n",
    "#使用线性层在波士顿房价数据集上建立模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#先导入相关库，因为数据量小，我们可以一次性学习一整个数据集，所以此处我们就不构建数据加载器了。\n",
    "from sklearn.datasets import load_boston\n",
    "import torch.nn as nn\n",
    "import torch\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "\n",
    "X, y = load_boston(return_X_y=True)\n",
    "# X.shape : (506, 13)\n",
    "# y.shape : (506,)\n",
    "# type : <class 'numpy.ndarray'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(506, 13)\n",
      "(506,)\n"
     ]
    }
   ],
   "source": [
    "print(X.shape)\n",
    "print(y.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    " #然后构建线性层类LR\n",
    "class LR(nn.Module):\n",
    "    def __init__(self, input_dim, output_dim):\n",
    "        super(LR, self).__init__()\n",
    "        self.fc = nn.Sequential(\n",
    "            nn.Linear(input_dim, 64),\n",
    "            nn.Linear(64, 128),\n",
    "            nn.Linear(128, 32),\n",
    "        )\n",
    "\n",
    "        self.regression = nn.Linear(32, 1)\n",
    "        \n",
    "    def forward(self, x):\n",
    "        out = self.fc(x)\n",
    "        out = self.regression(out)\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    " #接下来，记得把输入数据转换成tensor，并获取训练需要的对象：\n",
    " # 数据类型转换\n",
    "X_t = torch.tensor(X, dtype=torch.float32)\n",
    "y_t = torch.tensor(y, dtype=torch.float32)\n",
    "\n",
    "# 网络实例化\n",
    "net = LR(input_dim=13, output_dim=1)\n",
    "# 获取损失函数\n",
    "loss_func = nn.MSELoss()\n",
    "# 获取优化器对象\n",
    "optimizer = torch.optim.Adam(net.parameters(), lr=0.0001)\n",
    "\n",
    "loss_all = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXsAAAD4CAYAAAANbUbJAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAjyElEQVR4nO3deXzV9Z3v8dcnCyErScgJYEggAUQBFTDigk4Xa1FrpZ2pitMFW+c6Czp2btvp2HZm2j6GuV1nxtpq671S7R1HBmtttXWt1esOhEXZJawJWwIJZCP75/5xDpBiQkK2k3N+7+fjkUdOvud3yOfnefg+v3x/38XcHRERiW8J0S5ARESGnsJeRCQAFPYiIgGgsBcRCQCFvYhIACRFuwCAvLw8nzx5crTLEBGJKWvWrDns7qG+HDsiwn7y5MmUlZVFuwwRkZhiZnv6eqy6cUREAqDXsDezQjN72cy2mNkmM7s70v59M9tqZu+a2ZNmlt3lNfeYWbmZbTOzBUNYv4iI9EFfruzbgS+5+/nAZcASM5sBvAjMcvcLgfeAewAizy0CZgLXAvebWeJQFC8iIn3Ta9i7+wF3Xxt5XA9sAQrc/QV3b48c9jYwMfJ4IbDc3VvcfRdQDswb/NJFRKSvzqrP3swmA3OAlac99QXg2cjjAqCiy3OVkbbT/607zKzMzMqqq6vPpgwRETlLfQ57M8sAngC+6O51Xdq/Trir59ETTd28/H2rrbn7g+5e6u6loVCfRg6JiEg/9WnopZklEw76R939V13aFwM3AFf7qeUzK4HCLi+fCOwfnHJFRKQ/+jIax4CHgC3u/m9d2q8Fvgrc6O5NXV7yFLDIzFLMrBiYBqwa3LLD9h09zg9f2MbeI029HywiEmB9ubKfD3wW2GBm6yNtXwN+B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j7D6JZ21CLLKg4wd3XmlkmsIbwPhm3Eefv/xnO/WbO8r2P5Sv7eUC5u+9091ZgOeEllyUOufurQM1pzYHZKKeH8w8Edz/g7msjj+sJL7hYQADe/zOc+1mL5bAvACq6/FxJP/8jxDAHXjCzNWZ2R7SLiYI+bZQT5+40s3cj3Txx141xOjObDMwBVhKw9/+0c4ezfO9jOeytm7bY7JPqv/nuPhe4DlgS+VNfguMBYAowm/BucT+MajVDzMwygCeAL7p7XbTrGU7dnPtZv/exHPaVQGGXnycS3gM3MNx9f+R7FfAk4a6tIAn0RjnufsjdO9y9E/jfxPH7b2bJhMPuUXf/VaQ5EO9/d+fen/c+lsN+NTDNzIrNbBSwiPDGKYFgZumRGzaYWTrwUWDjmV8VdwK9Uc6JoIv4JHH6/kc2R3oI2OLu/9blqbh//3s69/689zE7GgcgMtzoP4BEYJm7L41uRcPHzEoIX81DeF+C/4rn8zezx4APEl7a9RDhfRR+DawAiohslOPucXkTs4fz/yDhP+Md2A385Yk+7HhiZlcCrwEbgM5I89cI913H9ft/hnO/lbN872M67EVEpG9iuRtHRET6SGEvIhIACnsRkQBQ2IuIBIDCXkQkABT2IiIBoLAXEQmA/w/UvYBOkXXZ0wAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    " #最后训练网络，并做数据可视化：\n",
    " # 开始训练\n",
    "for epoch in range(25):\n",
    "    # 前向传播\n",
    "    output = net(X_t)\n",
    "    # 计算损失\n",
    "    loss = loss_func(output.flatten(), y_t.flatten())\n",
    "    # 梯度清空\n",
    "    optimizer.zero_grad()\n",
    "    # 反向传播\n",
    "    loss.backward()\n",
    "    # 更新网络\n",
    "    optimizer.step()\n",
    "\n",
    "    loss_all.append(loss.item())\n",
    "\n",
    "data = pd.DataFrame({\"loss\" : loss_all}, columns=[\"loss\"])\n",
    "data[\"loss\"].plot(x=\"iteration\", y=\"loss\")\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "a8f61be024eba58adef938c9aa1e29e02cb3dece83a5348b1a2dafd16a070453"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit ('base': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
